//
//  FlowAddCell.swift
//  Employee
//
//  Created by swordjoy on 2017/6/28.
//  Copyright © 2017年 remotevillage. All rights reserved.
//

import UIKit
import Photos

protocol FlowAddCellDelegate: class {
  func addPicture()
  func deletePicture(index: Int)
  func clickPicture(index: Int)
}

class FlowAddCell: UITableViewCell {

  static let identifier = "FlowAddCell.swordjoy"
  
  weak var delegate: FlowAddCellDelegate?
    
    var assets: [PHAsset] = [] {
        didSet {
            collectionView.reloadData()
        }
    }
  
  fileprivate var addLabel: UILabel!
  fileprivate var collectionView: UICollectionView!
  fileprivate var tipsLabel: UILabel!
  
  // MARK: - life cycle
  override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
    super.init(style: style, reuseIdentifier: reuseIdentifier)
    selectionStyle = .none
    backgroundColor = UIColor.white
    create()
  }
  
  required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }
}

// MARK: - fileprivate

extension FlowAddCell {
  
  fileprivate func create() {
    addLabel = UILabel()
    addSubview(addLabel)
    addLabel.font = GlobalConfig.system12Font
    addLabel.textColor = GlobalConfig.TextLightColor
    addLabel.text = "附件(选填)"
    addLabel.snp.makeConstraints { (make) in
      make.left.equalTo(15)
      make.top.equalTo(20)
    }
    
    let flow = UICollectionViewFlowLayout()
    let width = (UIScreen.width - 80) / 4
    flow.itemSize = CGSize(width: width, height: width)
    flow.minimumInteritemSpacing = 10
    
    collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: flow)
    collectionView.backgroundColor = UIColor.clear
    collectionView.contentInset = UIEdgeInsets(top: 15, left: 15, bottom: 0, right: 15)
    addSubview(collectionView)
    collectionView.dataSource = self
    collectionView.delegate = self
    collectionView.register(ImageSelectCell.self, forCellWithReuseIdentifier: ImageSelectCell.identifier)
    collectionView.register(ChooseImageCell.self, forCellWithReuseIdentifier: ChooseImageCell.identifier)
    collectionView.snp.makeConstraints { (make) in
      make.left.right.equalToSuperview()
      make.top.equalTo(addLabel.snp.bottom).offset(10)
      make.bottom.equalTo(-40)
    }

    tipsLabel = UILabel()
    addSubview(tipsLabel)
    tipsLabel.font = GlobalConfig.system12Font
    tipsLabel.textColor = GlobalConfig.TextLightColor
    tipsLabel.text = "最多添加8个附件"
    tipsLabel.snp.makeConstraints { (make) in
      make.left.equalTo(15)
      make.bottom.equalTo(-18)
    }
  }
}

extension FlowAddCell: UICollectionViewDataSource {
  func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return assets.count < 8 ? assets.count + 1 : 8
  }
  
  func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
    // 数量小于4,增加cell才会出现
    if indexPath.row == assets.count {
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ChooseImageCell.identifier, for: indexPath) as! ChooseImageCell
      cell.addPhotoClosure = { [unowned self] in
        self.delegate?.addPicture()
      }
      return cell
    }
    
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: ImageSelectCell.identifier, for: indexPath) as! ImageSelectCell
    
    cell.deleteClosure = { [unowned self] (index) in
      self.assets.remove(at: index)
      collectionView.reloadData()
      self.delegate?.deletePicture(index: index)
    }
    cell.asset = assets[indexPath.row]
    cell.index = indexPath.row
    cell.backgroundColor = UIColor.red
    return cell
  }
}

extension FlowAddCell: UICollectionViewDelegate {
  
  func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
    delegate?.clickPicture(index: indexPath.row)
  }
  
}
